﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace DevelopTool.Forms.CommonForms
{
    /// <summary>
    /// 菜单选择窗口
    /// </summary>
    public partial class ChoiceMenuForm : BaseUIForm
    {
        #region 事件
        //事件参数
        public delegate void OperationHandler(object sender,MenuEventArgs e);

        /// <summary>
        /// 新增菜单节点事件，NodeType 1=目录,2=节点
        /// </summary>
        [Description("新增菜单节点事件"), Category("DevelopTool")]
        public event OperationHandler MenuAdd;

        #endregion 事件

        #region 公有属性

        private string titleText;

        /// <summary>
        /// 窗口标题文本
        /// </summary>
        public string TitleText
        {
            get
            {
                return this.titleText;
            }
            set
            {
                this.titleText = value;
            }
        }


        private string foldereText = "目录";

        /// <summary>
        /// 目录文本
        /// </summary>
        public string FolderText
        {
            get
            {
                return this.foldereText;
            }
            set
            {
                this.foldereText = value;
            }
        }

        private string menuName;

        /// <summary>
        /// 菜单默认名称
        /// </summary>
        public string MenuDefaultName
        {
            get
            {
                return this.menuName;
            }
            set
            {
                this.menuName = value;
            }
        }


        private IEnumerable<ManageMenu> manageMenus;

        /// <summary>
        /// 菜单集合
        /// </summary>
        public IEnumerable<ManageMenu> Menus
        {
            get
            {
                return this.manageMenus;
            }
            set
            {
                this.manageMenus = value;
            }
        }

        /// <summary>
        /// 顶级节点文本
        /// </summary>
        private string topTreeNodeText = "列表";
        /// <summary>
        /// 顶级节点文本
        /// </summary>
        public string TopTreeNodeText
        {
            get
            {
                return this.topTreeNodeText;
            }
            set
            {
                this.topTreeNodeText = value;
            }
        }

        private bool navMenuExpandAll = false;
        /// <summary>
        /// 是否展开所有菜单节点
        /// </summary>
        public bool NavMenuExpandAll
        {
            get
            {
                return this.navMenuExpandAll;
            }
            set
            {
                this.navMenuExpandAll = value;
            }
        }

        #endregion 公有属性

        #region 私有属性
        /// <summary>
        ///顶部节点，系统预定义
        /// </summary>
        private TreeNode topNode;

        /// <summary>
        /// 是否允许修改名称
        /// </summary>
        private bool isAllowModifyName = true;

        /// <summary>
        /// 是否允许添加节点到顶级节点
        /// </summary>
        private bool isAllowAddToTopNode = true;

        #endregion 私有属性

        public ChoiceMenuForm()
        {
            InitializeComponent();
        }

        public ChoiceMenuForm(string titleText)
        {
            InitializeComponent();

            this.titleText = titleText;
        }


        /// <summary>
        /// 菜单选择窗口
        /// </summary>
        /// <param name="titleText">窗口标题文本</param>
        /// <param name="menuDefaultName">节点默认名称</param>
        /// <param name="menus">菜单集合</param>
        /// <param name="isAllowModifyName">是否允许修改名称</param>
        /// <param name="isAllowAddToTopNode">是否允许添加节点到顶级节点</param>
        public ChoiceMenuForm(string titleText,string menuDefaultName,IEnumerable<ManageMenu> menus,bool isAllowModifyName = true,bool isAllowAddToTopNode = true)
        {
            InitializeComponent();

            this.titleText = titleText;
            this.menuName = menuDefaultName;
            this.manageMenus = menus;
            this.isAllowModifyName = isAllowModifyName;
            this.isAllowAddToTopNode = isAllowAddToTopNode;
        }


        private void ChoiceMenuForm_Load(object sender,EventArgs e)
        {
            this.Text = titleText;

            this.textBoxNoteName.Text = this.menuName;

            this.uiNavMenu.ClearAll();

            //创建顶级节点
            var topMenu = new ManageMenu() { Id = 0,NodeType = MenuNodeType.Folder };
            topNode = this.uiNavMenu.CreateNode(this.topTreeNodeText,new Random().Next());
            topNode.Name = "TopNode";
            topNode.Tag = new ManageMenu() { Id = 0,NodeType = MenuNodeType.Folder };

            this.BindMenuData(topNode,this.manageMenus);

            topNode.Expand();

            this.textBoxNoteName.ReadOnly = !this.isAllowModifyName;

            if(this.navMenuExpandAll) this.uiNavMenu.ExpandAll();

            ThemHelper.SetSystemThem(this);
        }



        /// <summary>
        /// 递归绑定菜单节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="list"></param>
        private void BindMenuData(TreeNode parentNode,IEnumerable<ManageMenu> list)
        {
            var parentMenu = parentNode.Tag as ManageMenu;

            foreach(ManageMenu menu in list.Where(it => it.NodePid == parentMenu.Id))
            {
                TreeNode node = null;
                if(parentNode == null)
                {
                    node = this.uiNavMenu.CreateNode(menu.NodeName,menu.Id);//父节点
                }
                else
                {
                    node = this.uiNavMenu.CreateChildNode(parentNode,menu.NodeName,menu.Id);//子节点                  
                }
                switch(menu.NodeType)
                {
                    case MenuNodeType.Folder://目录
                        node.Name = "Folder";
                        break;
                    case MenuNodeType.Node://节点
                        node.Name = "Node";
                        break;
                }

                node.Tag = menu;

                SetNodeDefaultSymbol(node);

                BindMenuData(node,list);

            }
        }


        /// <summary>
        /// 根据节点类型设置默认的图标
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private void SetNodeDefaultSymbol(TreeNode node)
        {
            switch(node.Name)
            {
                case "TopNode"://顶级节点
                    this.uiNavMenu.SetNodeSymbol(node,61641);
                    break;
                case "Folder"://目录节点
                    this.uiNavMenu.SetNodeSymbol(node,61564);
                    break;
                case "Node"://子节点
                    this.uiNavMenu.SetNodeSymbol(node,361788);
                    break;
                default:
                    this.uiNavMenu.SetNodeSymbol(node,61462);
                    break;
            }
        }


        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender,EventArgs e)
        {
            if(!this.isAllowAddToTopNode)
            {
                if(this.uiNavMenu.SelectedNode == null)
                {
                    this.ShowWarningNotifier("请选择目录节点");
                    return;
                }

                if(this.uiNavMenu.SelectedNode.Name == "TopNode") return;
            }

            TreeNode selectMenuNode = this.uiNavMenu.SelectedNode ?? this.topNode;//当前选中的节点
            ManageMenu manageMenu = selectMenuNode.Tag as ManageMenu;

            ManageMenu newMenu = new ManageMenu()
            {
                NodeName = textBoxNoteName.Text,
                NodeType = MenuNodeType.Node,
                NodePid = manageMenu.Id,
            };

            //触发新建项事件
            var args = new MenuEventArgs() { ManageMenu = newMenu,TreeNode = null };
            MenuAdd?.Invoke(sender,args);
            if(args.Cancel) return;//取消操作

            DialogResult = DialogResult.OK;
        }


        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender,EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }


        /// <summary>
        /// 新建目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateFolder_Click(object sender,EventArgs e)
        {
            if(!this.isAllowAddToTopNode)
            {
                if(this.uiNavMenu.SelectedNode == null)
                {
                    this.ShowWarningNotifier("请选择目录节点");
                    return;
                }

                if(this.uiNavMenu.SelectedNode.Name == "TopNode") return;
            }

            TreeNode selectMenuNode = this.uiNavMenu.SelectedNode ?? this.topNode;//当前选中的节点
            ManageMenu selectMenu = selectMenuNode.Tag as ManageMenu;//当前选中节点的Menu对象

            string nodeName = this.foldereText + DateTime.Now.DateTimeString(false);
            if(!this.ShowInputStringDialog(ref nodeName,true,$"请输入{this.foldereText}名：",false)) return;

            ManageMenu newMenu = new ManageMenu()
            {
                NodeName = nodeName,
                NodeType = MenuNodeType.Folder,
            };

            TreeNode parentNode;
            if(selectMenu.NodeType == MenuNodeType.Folder)//选中的是目录
            {
                newMenu.NodePid = selectMenu.Id;//设置父节点id
                parentNode = selectMenuNode;//父节点
            }
            else//选中的是节点
            {
                newMenu.NodePid = selectMenu.NodePid;
                parentNode = selectMenuNode.Parent;
            }

            //触发新建项事件
            var args = new MenuEventArgs() { ManageMenu = newMenu,TreeNode = null };
            MenuAdd?.Invoke(sender,args);
            if(args.Cancel) return;//取消操作
            newMenu = args.ManageMenu;

            //创建目录节点
            TreeNode newTreeNode = uiNavMenu.CreateChildNode(parentNode,newMenu.NodeName,new Random().Next());
            newTreeNode.Tag = newMenu;
            newTreeNode.Name = "Folder";

            SetNodeDefaultSymbol(newTreeNode);

            parentNode.Expand();//展开父节点

            this.uiNavMenu.SelectedNode = newTreeNode;//选中新建的节点

            this.ShowSuccessNotifier($"已创建{this.foldereText}：{newMenu.NodeName}");
        }


        private void uiNavMenu_BeforeCollapse(object sender,TreeViewCancelEventArgs e)
        {
            if(e.Node.Name == "TopNode")//不允许折叠顶级节点
            {
                e.Cancel = true;
            }
        }
    }
}
